/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package engine.entities.inventory.container;

import engine.entities.Entity;
import engine.entities.Player;
import engine.entities.inventory.ContainerInventory;
import engine.game.Game;
import engine.game.gfx.ColoredSprite;
import engine.game.gfx.Screen;
import engine.game.hud.StatBlockViewer.ViewerAction;
import java.awt.Point;

/**
 *
 * @author NathanT
 */
public abstract class ItemContainer extends Entity {

    private final Game game = Game.getGlobalGame();
    protected ContainerInventory inventory;
    protected ColoredSprite sprite;
    
    protected final ViewerAction[] viewActions;
    
    public ItemContainer(String name, ColoredSprite sprite, ContainerInventory inventory) {
        super(name);
        this.sprite = sprite;
        this.inventory = inventory;
        viewActions = buildViewerActions();
    }
    
    public ContainerInventory getInventory() {
        return inventory;
    }

    @Override
    public void tick() {
        
    }

    @Override
    public void render(Screen screen) {
        int xOffset = xOff + (this.coords.x * level.TILE_SIZE);
        int yOffset = yOff + (this.coords.y * level.TILE_SIZE);

        screen.render(xOffset, yOffset, sprite, SCALE);
    }
    
    @Override
    public boolean isViewable() {
        return true;
    }
    
    @Override
    public ColoredSprite getSprite() {
        return sprite;
    }

    @Override
    public int[][] pixels() {
        return sprite.pixels();
    }
    
    @Override
    public ViewerAction[] getViewerActions() {
        return viewActions;
    }
    
    private ViewerAction[] buildViewerActions() {
        ViewerAction[] actions = new ViewerAction[1];
        actions[0] = new OpenContainerAction(this);
        return actions;
    }
    
    @Override
    public ViewerAction getDefaultAction() {
        return null;
    }
    
    private class OpenContainerAction extends ViewerAction {
        
        private static final String NAME = "Open";
        private final ItemContainer container;
        
        public OpenContainerAction(ItemContainer container) {
            super(NAME);
            this.container = container;
        }

        @Override
        public boolean canBeInvoked() {
            boolean canInvoke = false;
            canInvoke = canInvoke || getLevel().entityAt(new Point(coords.x, coords.y + 1)) instanceof Player;
            canInvoke = canInvoke || getLevel().entityAt(new Point(coords.x, coords.y - 1)) instanceof Player;
            canInvoke = canInvoke || getLevel().entityAt(new Point(coords.x + 1, coords.y)) instanceof Player;
            canInvoke = canInvoke || getLevel().entityAt(new Point(coords.x - 1, coords.y)) instanceof Player;
            return canInvoke;
        }

        @Override
        public void performAction() {
            game.openContainer(container);
        }
        
    }
    
}
